package Servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
import com.mysql.jdbc.Connection;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.jms.Session;
import javax.servlet.http.HttpSession;
import Entidades.*;
import java.lang.Exception;
import java.util.ArrayList;

/**
 *
 * @author Martin
 */
@WebServlet(name = "creacion", urlPatterns = {"/creacion"})
public class creacion extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Statement st;
            //Persona p = new Persona();
            // Barrio b = new Barrio();
            String user = request.getParameter("txtUsuario");
            String contra = request.getParameter("txtPass");
            String nombre = request.getParameter("txtNombre");
            String apellido = request.getParameter("txtApellido");
            int nroDoc = Integer.parseInt(request.getParameter("txtNroDocumento"));
            String estCivil = request.getParameter("selectEstadoCivil");
            String calle = request.getParameter("txtCalle");
            String nroCalle = request.getParameter("txtNroCalle");
            String diaNac = request.getParameter("selectDia");
            String mesNac = request.getParameter("selectMes");
            String anioNac = request.getParameter("selectAnio");
            String email = request.getParameter("txtEmail");
            String nombreTipoDoc = request.getParameter("selectTipoDoc");
            TipoDoc tipoDoc = new TipoDoc(nombreTipoDoc);
            String sexo = request.getParameter("selectSexo");
            String barrio = request.getParameter("txtBarrio");
            String estadoCivil = request.getParameter("selectEstadoCivil");
            ArrayList telefonos = new ArrayList();
            long nroTel;
            String tipoTel = "";
            Barrio b = new Barrio();
            int nombreBarrio = b.getUltimaId();
            int cantTel = Integer.parseInt(request.getParameter("nroTels"));
            for (int i = 0; i <= cantTel; i++) {
                nroTel = Long.parseLong(request.getParameter("txtTelefono" + i));
                tipoTel = request.getParameter("selectTipoTel" + i);
                telefonos.add(new Telefono(tipoTel, nroTel));
            }
            String localidad = request.getParameter("selectNomLocalidad0");
            String prov = request.getParameter("selectNomProvincia0");
            if (Integer.parseInt(diaNac) < 10) {
                diaNac = "0" + diaNac;
            }
            String fechaNac = anioNac + "-" + mesNac + "-" + diaNac;
            Usuario u = new Usuario();
            if (u.validarUsuarioExistente(user)) {
                Usuario Unuevo = new Usuario(user, contra, true);
                Persona p = new Persona(apellido, nombre, nroDoc, tipoDoc, estadoCivil, nroCalle, calle, sexo, fechaNac, email);
                p.setUsuario(Unuevo);
                p.setTelefonos(telefonos);
                p.setBarrio(new Barrio(Integer.toString(nombreBarrio), new Localidad(localidad, new Provincia(prov)), barrio));
                if (p.insertarPersona()) {
                    request.getRequestDispatcher("registracionOk.jsp").forward(request, response);
                }
            }
            
            //DESPUES SETEAR SOLAMENTE EL USUARIO Y LOS TELEFONOS Y DESPUES LLAMAR AL METODO p.insertarPersona();
            //creamos la persona


            //Creamos conexion a la BD
            // Class.forName("com.mysql.jdbc.Driver");
            //Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/tpare", "root", "");

            //Creamos el statement para hacer consultas a la BD
            // st = conexion.createStatement();


            //El resultSet nos guarda lo que trae el statement
            // int ban=st.executeUpdate("INSERT into usuario VALUES('"+user+"', '"+contra+"'");
            //int ban2=st.executeUpdate("Insert intp persona values('"+tipoDoc+"','"+nroDoc+"','"+nombre+"','"+apellido+"','"+estCivil+"','"+calle+"','"+nroCalle+"','"+sexo+"','");
            // if(ban!=0) //si la bandera es distinta de 0, todo esta bien
            // {
            //    request.setAttribute("user", "El usuario "+user+" se creo satisfactoriamente");
            //}


        } catch (Exception ex) {
            Logger.getLogger(creacion.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(creacion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(creacion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
